<?php

namespace app\manage\controller;

use think\Db;
use app\manage\model\Advantage as AdvantageModel;

class Advantage extends Common
{
    public function index($tab = '1', $id = 0)
    {
        $advantageModel = new AdvantageModel();
        $advantage = $advantageModel->select();
        $this->assign('advantage', $advantage);
        //编辑菜单
        if (3 == $tab) {
            $advantage_info = Db::name('advantage')->where('id', $id)->find();
            if ($advantage_info != null && is_array($advantage_info)) {
                $this->assign('advantage_info', $advantage_info);
            }
        }
        return view();
    }

    //新增
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $advantageModel = new AdvantageModel();
            if ($advantageModel->save($data)) {
                return success('添加成功', url('index', ['tab' => 1]));
            } else {
                return error('添加失败');
            }
        }
    }

    //修改信息
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $advantageModel = new AdvantageModel();
            if ($advantageModel->allowField(true)->isUpdate()->save($data)) {
                return success('编辑成功!', url('index', ['tab' => 1]));
            } else {
                return error('编辑失败或者是未修改!');
            }
        }
    }

    public function delete($id)
    {
        $advantageModel = new AdvantageModel();
        $advantage = $advantageModel::destroy($id);
        if ($advantage) {
            return success('删除成功！', url('index', ['tab' => 1]));
        } else {
            return error('删除失败！');
        }
    }

}
